using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestGUI
{
	/// <summary>
	/// This class is the form from which an admin can view every manager account
	///  in the database, and add, remove, or edit such managers.
	/// </summary>
	public class ManagerList : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox ManagerGroup;
		private System.Windows.Forms.Button addButton;
		private System.Windows.Forms.Button removeButton;
		private System.Windows.Forms.Button editPassButton;
		private System.Windows.Forms.Button closeButton;
		Session ses;
		private System.Windows.Forms.ListBox managerBox;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ManagerList()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		public ManagerList(Session s) 
		{
			InitializeComponent();
			ses = s;
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			this.Owner.Visible = true;
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.ManagerGroup = new System.Windows.Forms.GroupBox();
			this.addButton = new System.Windows.Forms.Button();
			this.managerBox = new System.Windows.Forms.ListBox();
			this.removeButton = new System.Windows.Forms.Button();
			this.editPassButton = new System.Windows.Forms.Button();
			this.closeButton = new System.Windows.Forms.Button();
			this.ManagerGroup.SuspendLayout();
			this.SuspendLayout();
			// 
			// ManagerGroup
			// 
			this.ManagerGroup.Controls.Add(this.addButton);
			this.ManagerGroup.Controls.Add(this.managerBox);
			this.ManagerGroup.Controls.Add(this.removeButton);
			this.ManagerGroup.Controls.Add(this.editPassButton);
			this.ManagerGroup.Location = new System.Drawing.Point(8, 8);
			this.ManagerGroup.Name = "ManagerGroup";
			this.ManagerGroup.Size = new System.Drawing.Size(288, 224);
			this.ManagerGroup.TabIndex = 9;
			this.ManagerGroup.TabStop = false;
			this.ManagerGroup.Text = "Manager";
			// 
			// addButton
			// 
			this.addButton.Location = new System.Drawing.Point(8, 192);
			this.addButton.Name = "addButton";
			this.addButton.Size = new System.Drawing.Size(88, 24);
			this.addButton.TabIndex = 1;
			this.addButton.Text = "Add";
			this.addButton.Click += new System.EventHandler(this.addButton_Click);
			// 
			// managerBox
			// 
			this.managerBox.Location = new System.Drawing.Point(8, 24);
			this.managerBox.Name = "managerBox";
			this.managerBox.Size = new System.Drawing.Size(272, 160);
			this.managerBox.TabIndex = 0;
			// 
			// removeButton
			// 
			this.removeButton.Location = new System.Drawing.Point(104, 192);
			this.removeButton.Name = "removeButton";
			this.removeButton.Size = new System.Drawing.Size(88, 24);
			this.removeButton.TabIndex = 1;
			this.removeButton.Text = "Remove";
			this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
			// 
			// editPassButton
			// 
			this.editPassButton.Location = new System.Drawing.Point(200, 192);
			this.editPassButton.Name = "editPassButton";
			this.editPassButton.Size = new System.Drawing.Size(80, 24);
			this.editPassButton.TabIndex = 1;
			this.editPassButton.Text = "Edit";
			this.editPassButton.Click += new System.EventHandler(this.editPassButton_Click);
			// 
			// closeButton
			// 
			this.closeButton.Location = new System.Drawing.Point(8, 248);
			this.closeButton.Name = "closeButton";
			this.closeButton.Size = new System.Drawing.Size(288, 24);
			this.closeButton.TabIndex = 10;
			this.closeButton.Text = "Close";
			this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
			// 
			// ManagerList
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(304, 278);
			this.Controls.Add(this.closeButton);
			this.Controls.Add(this.ManagerGroup);
			this.Name = "ManagerList";
			this.Text = "ManagerList";
			this.Load += new System.EventHandler(this.ManagerList_Load);
			this.ManagerGroup.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void closeButton_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void addButton_Click(object sender, System.EventArgs e)
		{
			Form f = new AddManager(ses);
			this.AddOwnedForm(f);
			f.Owner = this;
			f.Visible = true;
			f.Activate();
			this.Visible = false;
		}

		private void removeButton_Click(object sender, System.EventArgs e)
		{

			if (managerBox.SelectedIndices.Count > 0) 
			{
				int index = managerBox.SelectedIndex;
				string name = managerBox.Items[index].ToString();
				if (MessageBox.Show("Are you sure you want to remove manager "+name+"?","Are you sure?",System.Windows.Forms.MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes) 
				{
					return;
				}
				if (ses.removeManager(name)) 
				{
					managerBox.Items.RemoveAt(index);
				}
				refreshList();
			}
		}

		private void editPassButton_Click(object sender, System.EventArgs e)
		{
			if (managerBox.SelectedIndices.Count > 0) 
			{
				int index = managerBox.SelectedIndex;
				string name = managerBox.Items[index].ToString();
				ses.setTempManager(name);
				Form f = new EditManager(ses);
				this.AddOwnedForm(f);
				f.Owner = this;
				f.Visible = true;
				f.Activate();
				this.Visible = false;

			}
		}

		public void refreshList() 
		{
			managerBox.Items.Clear();
			string[] mans = ses.getManagerList();
			if (mans == null) 
			{
				return;
			}
			int i = 0;
			while (i < mans.Length && mans[i] != null) 
			{
				managerBox.Items.Add(mans[i]);
				i++;
			}
		}
	
		private void ManagerList_Load(object sender, System.EventArgs e)
		{
			string[] mans = ses.getManagerList();
			int i = 0;
			if (mans == null) 
			{
				return;
			}
			while (i < mans.Length && mans[i] != null) 
			{
				managerBox.Items.Add(mans[i]);
				i++;
			}
		}
	}
}
